package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.localbean.WxCallBackData;
import com.wgts.wgts.mvp.model.ILoginActivityModel;
import com.wgts.wgts.mvp.presenter.ILoginActivityPersenter;
import com.wgts.wgts.mvp.view.ILoginActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.WXLoginRequest;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.react.MD5Util;

public class LoginActivityPersenter implements ILoginActivityPersenter {

    private ILoginActivityModel model;

    private ILoginActivityView view;


    public LoginActivityPersenter(ILoginActivityModel model, ILoginActivityView view) {
        this.model = model;
        this.view = view;
    }

    public void wxLogin(WxCallBackData backData){
        WXLoginRequest wxLoginRequest = new WXLoginRequest();
        wxLoginRequest.avatar = backData.getIconurl();
        wxLoginRequest.nickName = backData.getName();
        wxLoginRequest.thirdparty = "WX";
        wxLoginRequest.uid = backData.getOpenid();
        wxLoginRequest.unionid = backData.getUnionid();
        model.wxLogin(wxLoginRequest, new HttpResultSubscriber<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity loginEntity) {
                view.wxLoginResult(loginEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void wgtsLogin(String userName, String password) {

        password = MD5Util.encrypt(password);

        model.wgtsLogin(userName, password, new HttpResultSubscriber<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity entity) {
                view.wgtsLoginResult(entity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
